/*
 * Copyright 2010 Michael Nitsche
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package de.mnit.basis.swt.dnd.transfer;

import org.eclipse.swt.dnd.ByteArrayTransfer;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.TransferData;

import de.mnit.basis.fehler.Fehler;


/**
 * @author Michael Nitsche
 * 29.11.2010	Erstellt
 */
public class TextFormatTransfer extends ByteArrayTransfer {

	private static final String TYPENAME = "TextFormat-Transfer";
	private static final int TYPEID = registerType(TYPENAME);
	private static TextFormatTransfer _instance = new TextFormatTransfer();


	public static TextFormatTransfer getInstance() {
		return _instance;
	}


	public void javaToNative(Object object, TransferData transferData) {
        if(!validate(object) || !isSupportedType(transferData))
            DND.error(DND.ERROR_INVALID_DATA);

        try {
        	TextFormatObjekt tfo=(TextFormatObjekt)object;
            super.javaToNative(TextFormatObjekt.zuByteArray(tfo), transferData);
        }
        catch(Exception e) {
        	Fehler.zeig(e, false);
        }
	}

	public Object nativeToJava(TransferData transferData) {
        if(!isSupportedType(transferData))
        	return null;

        byte[] ba = (byte[]) super.nativeToJava(transferData);
        if(ba == null)
        	return null;

        TextFormatObjekt tfo=null;
        try {
        	tfo=TextFormatObjekt.zuObjekt(ba);
        }
        catch(Exception e) {
        	Fehler.zeig(e);
        }
        return tfo;
	}

	protected String[] getTypeNames() {
		return new String[] { TYPENAME };
	}

	protected int[] getTypeIds() {
		return new int[] { TYPEID };
	}

	protected boolean validate(Object object) {
        if(object == null || !(object instanceof TextFormatObjekt))
        	return false;
        return super.validate(object);
	}
}

